home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / Math / pmlerr.c < prev    next >
C/C++ Source or Header  |  1990-04-07  |  9KB  |  312 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *                              N O T I C E                             *
  4.  *                                                                      *
  5.  *                      Copyright Abandoned, 1987, Fred Fish            *
  6.  *                                                                      *
  7.  *      This previously copyrighted work has been placed into the       *
  8.  *      public domain by the author (Fred Fish) and may be freely used  *
  9.  *      for any purpose, private or commercial.  I would appreciate     *
  10.  *      it, as a courtesy, if this notice is left in all copies and     *
  11.  *      derivative works.  Thank you, and enjoy...                      *
  12.  *                                                                      *
  13.  *      The author makes no warranty of any kind with respect to this   *
  14.  *      product and explicitly disclaims any implied warranties of      *
  15.  *      merchantability or fitness for any particular purpose.          *
  16.  *                                                                      *
  17.  ************************************************************************
  18.  */
  19.  
  20. /************************************************************************
  21.  *                                                                      *
  22.  *              PORTABLE MATH LIBRARY -- ERROR HANDLER                  *
  23.  *                                                                      *
  24.  *      This is a sample PML library error handler.                     *
  25.  *      It may be used as is, or another of the user's choice           *
  26.  *      substituted.                                                    *
  27.  *                                                                      *
  28.  *      In any case, the global "pmlerr" must be defined somewhere      *
  29.  *      in the user task, since many Portable Math Library routines     *
  30.  *      reference it.  The other routines in this file are not called   *
  31.  *      by any library routines and may be omitted.                     *
  32.  *                                                                      *
  33.  ************************************************************************
  34.  */
  35.  
  36. #include <stdio.h>
  37. #include "pml.h"
  38.  
  39. struct pml_err {
  40.     int flags;                  /* Flag word; bits defined in pml.h     */
  41.     char *msg;                  /* Error message                        */
  42.     char *func;                 /* Function in which error occured      */
  43. };
  44.  
  45. static 
  46. struct pml_err pml_errs[] = {
  47.     CONTINUE | COUNT | LOG, "overflow", "exp",
  48.     CONTINUE | COUNT | LOG, "underflow", "exp",
  49.     CONTINUE | COUNT | LOG, "exponent overflow", "scale",
  50.     CONTINUE | COUNT | LOG, "negative argument", "sqrt",
  51.     CONTINUE | COUNT | LOG, "zero argument", "log",
  52.     CONTINUE | COUNT | LOG, "negative argument", "log",
  53.     CONTINUE | COUNT | LOG, "argument magnitude greater than 1.0", "acos",
  54.     CONTINUE | COUNT | LOG, "argument magnitude greater than 1.0", "asin",
  55.     CONTINUE | COUNT | LOG, "overflow", "tan",
  56.     CONTINUE | COUNT | LOG, "overflow", "cosh",
  57.     CONTINUE | COUNT | LOG, "underflow", "cosh",
  58.     CONTINUE | COUNT | LOG, "overflow", "sinh",
  59.     CONTINUE | COUNT | LOG, "underflow", "sinh",
  60.     CONTINUE | COUNT | LOG, "overflow", "asinh",
  61.     CONTINUE | COUNT | LOG, "argument less than 1.0", "acosh",
  62.     CONTINUE | COUNT | LOG, "overflow", "acosh",
  63.     CONTINUE | COUNT | LOG, "argument magnitude not  < 1.0", "atanh",
  64.     CONTINUE | COUNT | LOG, "underflow", "atan",
  65.     CONTINUE | COUNT | LOG, "complex division by zero", "cdiv",
  66.     CONTINUE | COUNT | LOG, "complex reciprocal of zero", "crcp",
  67.     CONTINUE | COUNT | LOG, "exponent underflow", "scale",
  68.     CONTINUE | COUNT | LOG, "argument has no fractional part", "dint",
  69. };
  70.  
  71. static int err_count = 0;               /* Counter for PML errors */
  72. static int err_limit = MAX_ERRORS;      /* PML error limit */
  73.  
  74. /*
  75.  *  FUNCTION
  76.  *
  77.  *      pmlcfs   Clear specified PML error handler flags
  78.  *
  79.  *  KEY WORDS
  80.  *
  81.  *      pmlcfs
  82.  *      machine independent routines
  83.  *      math libraries
  84.  *
  85.  *  DESCRIPTION
  86.  *
  87.  *      Clear the specified PML error handler flags for the
  88.  *      specified error.  Two or more flags may be cleared simultaneously
  89.  *      by "or-ing" them in the call, for example "LOG | CONTINUE".
  90.  *      The manifest constants for the flags and error codes are
  91.  *      defined in <pmluser.h>.
  92.  *
  93.  *  USAGE
  94.  *
  95.  *      pmlcfs(err_code,flags)
  96.  *      int err_code;
  97.  *      int flags;
  98.  *
  99.  *  PROGRAMMER
  100.  *
  101.  *      Fred Fish
  102.  *      Tempe, Az 85281
  103.  *      (602) 966-8871
  104.  *
  105.  */
  106.  
  107. pmlcfs(err_code,flag_word)
  108. register int err_code;
  109. register int flag_word;
  110. {
  111.     if (err_code < 0 || err_code > (sizeof(pml_errs)/sizeof(struct pml_err))) {
  112.         fprintf(stderr,"pmlcfs: invalid error code %d\n",err_code);
  113.     } else {
  114.         pml_errs[err_code].flags &= ~flag_word;
  115.     }
  116. }
  117.  
  118. /*
  119.  *  FUNCTION
  120.  *
  121.  *      pmlcnt   get PML error count and reset it to zero
  122.  *
  123.  *  KEY WORDS
  124.  *
  125.  *      pmlcnt
  126.  *      machine independent routines
  127.  *      math libraries
  128.  *
  129.  *  DESCRIPTION
  130.  *
  131.  *      Returns the total number of PML errors seen
  132.  *      prior to the call, and resets the error count to zero.
  133.  *
  134.  *  USAGE
  135.  *
  136.  *      int pmlcnt()
  137.  *
  138.  *  PROGRAMMER
  139.  *
  140.  *      Fred Fish
  141.  *      Tempe, Az 85281
  142.  *      (602) 966-8871
  143.  *
  144.  */
  145.  
  146. int pmlcnt()
  147. {
  148.     register int rtn_val;
  149.  
  150.     rtn_val = err_count;
  151.     err_count = 0;
  152.     return(rtn_val);
  153. }
  154.  
  155. /*
  156.  *  FUNCTION
  157.  *
  158.  *      pmlerr   Portable Math Library error handler
  159.  *
  160.  *  KEY WORDS
  161.  *
  162.  *      pmlerr
  163.  *      machine independent routines
  164.  *      math libraries
  165.  *
  166.  *  DESCRIPTION
  167.  *
  168.  *      Provides a sample PML error handler.  Does
  169.  *      not use any available hardware "traps" so is machine
  170.  *      independent.  Generally only called internally by the
  171.  *      other PML routines.
  172.  *
  173.  *      There are currently three flags which control the
  174.  *      response for specific errors:
  175.  *
  176.  *       (1)  LOG      When set an error message is sent
  177.  *                     to the user terminal.
  178.  *
  179.  *       (2)  COUNT    When set the error is counted
  180.  *                     against the PML error limit.
  181.  *
  182.  *       (3) CONTINUE  When set the task continues
  183.  *                     providing the error count has not
  184.  *                     exceeded the PML error limit.
  185.  *
  186.  *      Each of these flags can be set or reset independently
  187.  *      by "pmlsfs" or "pmlcfs" respectively.
  188.  *
  189.  *  USAGE
  190.  *
  191.  *      pmlerr(err_code)
  192.  *      int err_code;
  193.  *
  194.  *  PROGRAMMER
  195.  *
  196.  *      Fred Fish
  197.  *      Tempe, Az 85281
  198.  *      (602) 966-8871
  199.  *
  200.  */
  201.  
  202. pmlerr(err_code)
  203. register int err_code;
  204. {
  205.     register struct pml_err *err;
  206.  
  207.     if (err_code < 0 || err_code > (sizeof(pml_errs)/sizeof(struct pml_err))) {
  208.         fprintf(stderr,"pmlerr: invalid error code %d\n",err_code);
  209.     } else {
  210.         err = &pml_errs[err_code];
  211.         if (err->flags & LOG) {
  212.             fprintf(stderr,"pml: %s in function \"%s\"\n",err->msg,err->func);
  213.         }
  214.         if (err->flags & COUNT) {
  215.             err_count++;
  216.         }
  217.         if ((err->flags & CONTINUE) && (err_count <= err_limit)) {
  218.             return;
  219.         } else {
  220.             fprintf(stderr,"pml: error limit exceeded\n");
  221.             fprintf(stderr,"pml: task aborted with %d error(s)\n",
  222.                 err_count);
  223.             exit(-1);
  224.         }
  225.     }
  226. }
  227.  
  228. /*
  229.  *  FUNCTION
  230.  *
  231.  *      pmllim   Set Portable Math Library error limit 
  232.  *
  233.  *  KEY WORDS
  234.  *
  235.  *      pmllim
  236.  *      machine independent routines
  237.  *      math libraries
  238.  *
  239.  *  DESCRIPTION
  240.  *
  241.  *      Sets the PML error limit to the specified value
  242.  *      and returns it previous value.
  243.  *      Does not affect the current error count (which may be reset
  244.  *      to zero by a call to "pmlcnt").  Note that the default error
  245.  *      limit is set at compile time by the value in "pml.h".
  246.  *
  247.  *  USAGE
  248.  *
  249.  *      int pmllim(limit)
  250.  *      int limit;
  251.  *
  252.  *  PROGRAMMER
  253.  *
  254.  *      Fred Fish
  255.  *      Tempe, Az 85281
  256.  *      (602) 966-8871
  257.  *
  258.  */
  259.  
  260. int pmllim(limit)
  261. register int limit;
  262. {
  263.     register int rtn_val;
  264.  
  265.     rtn_val = err_limit;
  266.     err_limit = limit;
  267.     return(rtn_val);
  268. }
  269.  
  270. /*
  271.  *  FUNCTION
  272.  *
  273.  *      pmlsfs   Set specified PML error handler flags
  274.  *
  275.  *  KEY WORDS
  276.  *
  277.  *      pmlsfs
  278.  *      machine independent routines
  279.  *      math libraries
  280.  *
  281.  *  DESCRIPTION
  282.  *
  283.  *      Set the specified PML error handler flags for the
  284.  *      specified error.  Two or more flags may be set simultaneously
  285.  *      by "or-ing" them in the call, for example "LOG | CONTINUE".
  286.  *      The manifest constants for the flags and error codes are
  287.  *      defined in <pmluser.h>.
  288.  *
  289.  *  USAGE
  290.  *
  291.  *      pmlsfs(err_code,flags)
  292.  *      int err_code;
  293.  *      int flags;
  294.  *
  295.  *  PROGRAMMER
  296.  *
  297.  *      Fred Fish
  298.  *      Tempe, Az 85281
  299.  *
  300.  */
  301.  
  302. pmlsfs(err_code,flag_word)
  303. register int err_code;
  304. register int flag_word;
  305. {
  306.     if (err_code < 0 || err_code > (sizeof(pml_errs)/sizeof(struct pml_err))) {
  307.         fprintf(stderr,"? pmlsfs --- invalid error code %d.\n",err_code);
  308.     } else {
  309.         pml_errs[err_code].flags |= flag_word;
  310.     }
  311. }
  312.